package com.das.mechanic_base.widget;

import android.content.Context;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.das.mechanic_base.R;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3Utils;
import com.umeng.analytics.MobclickAgent;

public class X3BottomTaskShortDialog extends X3BaseBottomSheetDialog implements View.OnClickListener,TextWatcher {

    private Button btn_create;
    private Button btn_cancel;
    private TextView tv_clear;
    private TextView tv_num;
    private EditText et_input;

    public X3BottomTaskShortDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_clear = getView(R.id.tv_clear);
        btn_create = getView(R.id.btn_create);
        btn_cancel = getView(R.id.btn_cancel);
        tv_num = getView(R.id.tv_num);
        et_input = getView(R.id.et_input);

        et_input.addTextChangedListener(this);
        et_input.setOnClickListener(this);
        btn_create.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);
        tv_clear.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_bottom_task_short_dialog;
    }

    @Override
    public void show() {
        super.show();
        mHandler.postDelayed(runnable,200);
        tv_clear.performClick();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_create) {// 创建或者修改待办事项
            if (!X3Utils.isFastClick()) {
                return;
            }
            String content = et_input.getText().toString();
            if (X3StringUtils.isEmpty(content)) {
                return;
            }
            if (iOnGetTaskShort != null) {
                iOnGetTaskShort.iOnGetTaskShort(content);
            }
            dismiss();
        } else if (id == R.id.tv_clear) {/**
         * 清空车主内容
         */
            et_input.setText("");
        } else if (id == R.id.btn_cancel) {
            dismiss();
        }
    }

    X3TaskInputShortWindow.IOnGetTaskShort iOnGetTaskShort;

    public void setiOnGetTaskShort(X3TaskInputShortWindow.IOnGetTaskShort iOnGetTaskShort) {
        this.iOnGetTaskShort = iOnGetTaskShort;
    }

    public interface IOnGetTaskShort {
        void iOnGetTaskShort(String content);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        String content = editable.toString();

        tv_clear.setVisibility(X3StringUtils.isEmpty(content) ? View.GONE : View.VISIBLE);
        if (X3StringUtils.isEmpty(content)) {
            tv_num.setText("0/50");
        }else {
            tv_num.setText(content.length() + "/50");
        }
    }

    private Handler mHandler = new Handler();

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            et_input.setFocusable(true);
            et_input.setFocusableInTouchMode(true);
            et_input.requestFocus();
            InputMethodManager inputManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.showSoftInput(et_input, 0);
        }
    };

    @Override
    public void dismiss() {
        if (mHandler != null) {
            mHandler.removeCallbacks(runnable);
        }
        super.dismiss();
    }
    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("添加快捷短语弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("添加快捷短语弹窗");
    }
}
